package com.cloud.bssp.thread.sync;

import org.openjdk.jol.info.ClassLayout;

import java.util.Vector;
import java.util.concurrent.locks.LockSupport;

/**
 * @description： TODO
 * @author：weirx
 * @date：2021/11/25 15:45
 * @version：3.0
 */
public class SynchronizedTest {
//
//
//    public static void main(String[] args) throws InterruptedException {
//
//        Object lock = new Object();
//        // 打印初始状态
//        System.out.println(ClassLayout.parseInstance(lock).toPrintable());
//
//
//        synchronized (lock) {
//            // 打印偏向锁状态
//            System.out.println(ClassLayout.parseInstance(lock).toPrintable());
//        }
//
//        TimeUnit.SECONDS.sleep(1);
//
//        new Thread(() -> {
//            synchronized (lock) {
//                // 打印轻量级锁
//                System.out.println(ClassLayout.parseInstance(lock).toPrintable());
//            }
//        }).start();
//
//        System.out.println(ClassLayout.parseInstance(lock).toPrintable());
//
//        //再次获取锁
//        while(true){
//            synchronized (lock) {
//                // 打印偏向锁状态
//                if (ClassLayout.parseInstance(lock).toPrintable().contains("biased: 0x")){
//                    System.out.println(ClassLayout.parseInstance(lock).toPrintable());
//                    return;
//                }
//            }
//        }
//    }


    static Thread t1, t2, t3;

    public static void main(String[] args) throws InterruptedException {
        test3();
    }

    public static String print(String s) {
        String[] split = s.split("\r\n");
        return split[2].substring(41);
    }

    private static void test3() throws InterruptedException {
        Vector<Object> list = new Vector<>();
        t1 = new Thread(() -> {
            for (int i = 0; i < 50; i++) {
                Object d = new Object();
                list.add(d);
                synchronized (d) {
                    System.out.println(i + "\t" + print(ClassLayout.parseInstance(d).toPrintable()));
                }
            }
            LockSupport.unpark(t2);
        }, "t1");
        t1.start();

        t2 = new Thread(() -> {
            LockSupport.park();
            System.out.println("=========第二阶段======> ");
            for (int i = 0; i < 50; i++) {
                Object d = list.get(i);
                System.out.println("t2: " + i + "\t" + print(ClassLayout.parseInstance(d).toPrintable()));
                synchronized (d) {
                    System.out.println("t2: " + i + "\t" + print(ClassLayout.parseInstance(d).toPrintable()));
                }
                System.out.println("t2: " + i + "\t" + print(ClassLayout.parseInstance(d).toPrintable()));
            }
            LockSupport.unpark(t3);
        }, "t2");
        t2.start();


        t3 = new Thread(() -> {
            LockSupport.park();
            System.out.println("=========第三阶段======>");
            for (int i = 0; i < 50; i++) {
                Object d = list.get(i);
                System.out.println("t3: " + i + "\t" + print(ClassLayout.parseInstance(d).toPrintable()));
                synchronized (d) {
                    System.out.println("t3: " + i + "\t" + print(ClassLayout.parseInstance(d).toPrintable()));
                }
                System.out.println("t3: " + i + "\t" + print(ClassLayout.parseInstance(d).toPrintable()));
            }
        }, "t3");
        t3.start();

        t3.join();
        // 输出一个新的对象的对象头，发现是不可偏向锁
        System.out.println(print(ClassLayout.parseInstance(new Object()).toPrintable()));
    }
}
